/*
 * Copyright (c) 2009 Roman Savelyev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef __CONUISURFACE_H__
#define __CONUISURFACE_H__

//#include "conuidef.h"
#include <e32base.h>

class CConUiEngine;
class CDrawingBuffer;
class CBlinker;

enum TColor
    {
    EColorPreserve,
    EColorDefault,
    EColorBlack,
    EColorRed,
    EColorGreen,
    EColorYellow,
    EColorBlue,
    EColorMagenta,
    EColorCyan,
    EColorWhite
    };

enum TBrushEffect
    {
    EBrushEffectPreserve,
    EBrushEffectDefault,
    EBrushEffectInvesrseVideo
    };

enum TPenEffect
    {
    EPenEffectPreserve,
    EPenEffectDefault,
    EPenEffectBold,
    EPenEffectBlinking,
    EPenEffectBoldBlinking
    };

enum TLineStyle
    {
    ELineStyleInvisible,
    ELineStyleSingle,
    ELineStyleDouble
    };

class TConUiBrush
    {
public:
    inline TConUiBrush(TColor aColor=EColorPreserve, TBrushEffect aEffect=EBrushEffectPreserve);
    inline TColor Color() const;
    inline void SetColor(TColor aColor);
    inline TBrushEffect Effect() const;
    inline void SetEffect(TBrushEffect aEffect);
private:
    TColor iColor;
    TBrushEffect iEffect;
    };

class TConUiPen
    {
public:
    inline TConUiPen(TColor aColor=EColorPreserve, TPenEffect aEffect=EPenEffectPreserve);
    inline TColor Color() const;
    inline void SetColor(TColor aColor);
    inline TPenEffect Effect() const;
    inline void SetEffect(TPenEffect aEffect);
private:
    TColor iColor;
    TPenEffect iEffect;
    };

NONSHARABLE_CLASS(CConUiSurface) : public CBase
    {
public:
    IMPORT_C static CConUiSurface* NewL(CConUiEngine& aEngine);
    IMPORT_C ~CConUiSurface();
    IMPORT_C const TRect& Rect() const;
    inline const TConUiBrush& Brush() const;
    inline TConUiBrush& Brush();
    inline const TConUiPen& Pen() const;
    inline TConUiPen& Pen();
    IMPORT_C void Clear(const TRect& aRect);
    IMPORT_C void Draw(const TPoint& aAt, const TDesC& aText);
    IMPORT_C void DrawLine(const TPoint& aFrom, const TPoint& aTo, TLineStyle aStyle);
    IMPORT_C void DrawRect(const TRect& aRect, TLineStyle aStyle);
    IMPORT_C void InvalidateL(const TRect& aRect);
    IMPORT_C void InvalidateL(const RRegion& aRegion);
    IMPORT_C void ValidateL(const TRect& aRect);
    IMPORT_C void ValidateL(const RRegion& aRegion);
    IMPORT_C void Blit(const CConUiSurface& aSource, const TRect& aRect);
    IMPORT_C void Blit(const CConUiSurface& aSource, const RRegion& aRegion);
    IMPORT_C void ShowCursor(const TPoint& aPosition);
    IMPORT_C TInt CursorPos(TPoint& aPosition) const;
    IMPORT_C TInt HideCursor();
private:
    CConUiSurface(CConUiEngine& aEngine);
    void ConstructL();
    static TInt ValidateCallback(TAny* aSelf);
    void DoValidateCallbackL();
    void FillCurrentBrushPen(const TRect& aRect);
private:
    friend class CBlinker;
    CConUiEngine& iEngine;
    CDrawingBuffer* iBuffer;
    CBlinker* iBlinker;
    CAsyncCallBack iValidateCallback;
    TConUiBrush iBrush;
    TConUiPen iPen;
    RRegion iInvalidated;
    RRegion iValidated;
    TPoint iCursorPosition;
    };
///////////////////////////////////////////////////////////////////////////////
inline TConUiBrush::TConUiBrush(TColor aColor, TBrushEffect aEffect) :
    iColor(aColor),
    iEffect(aEffect)
    {}

inline TColor TConUiBrush::Color() const
    {
    return iColor;
    }

inline void TConUiBrush::SetColor(TColor aColor)
    {
    iColor = aColor;
    }

inline TBrushEffect TConUiBrush::Effect() const
    {
    return iEffect;
    }

inline void TConUiBrush::SetEffect(TBrushEffect aEffect)
    {
    iEffect = aEffect;
    }
///////////////////////////////////////////////////////////////////////////////
inline TConUiPen::TConUiPen(TColor aColor, TPenEffect aEffect) :
    iColor(aColor),
    iEffect(aEffect)
    {}

inline TColor TConUiPen::Color() const
    {
    return iColor;
    }

inline void TConUiPen::SetColor(TColor aColor)
    {
    iColor = aColor;
    }

inline TPenEffect TConUiPen::Effect() const
    {
    return iEffect;
    }

inline void TConUiPen::SetEffect(TPenEffect aEffect)
    {
    iEffect = aEffect;
    }
///////////////////////////////////////////////////////////////////////////////
inline const TConUiBrush& CConUiSurface::Brush() const
    {
    return iBrush;
    }

inline TConUiBrush& CConUiSurface::Brush()
    {
    return iBrush;
    }

inline const TConUiPen& CConUiSurface::Pen() const
    {
    return iPen;
    }

inline TConUiPen& CConUiSurface::Pen()
    {
    return iPen;
    }

#endif /* __CONUISURFACE_H__ */
